package cn.xspace.module.other.http;

import com.xspace.android.xafcommon.http.resp.BaseGroupRelResp;
import com.xspace.android.xafcommon.network.message.AResponseMsg;
import com.xspace.android.xafcommon.network.message.ControlManage;
import com.xspace.android.xafcommon.network.message.IResponseMsg;
import com.xspace.android.xafcommon.network.message.SendRequestListener;

import cn.xspace.module.other.http.req.DetalHtmlReq;
import cn.xspace.module.other.http.req.FetchVertifyCodeReq;
import cn.xspace.module.other.http.req.ForgetPasswordReq;
import cn.xspace.module.other.http.req.ImgDownReq;
import cn.xspace.module.other.http.req.ModifyUserInfoReq;
import cn.xspace.module.other.http.req.PlaceSetReq;
import cn.xspace.module.other.http.req.QueryAppIndexReq;
import cn.xspace.module.other.http.req.QueryInitialDataReq;
import cn.xspace.module.other.http.req.QueryLoginInfoReq;
import cn.xspace.module.other.http.req.QueryUnionAreaReq;
import cn.xspace.module.other.http.req.QueryUserInfoReq;
import cn.xspace.module.other.http.req.RegisterReq;
import cn.xspace.module.other.http.req.ResetPasswordReq;
import cn.xspace.module.other.http.req.UpgradeReq;
import cn.xspace.module.other.http.resp.DetailHtmlResp;
import cn.xspace.module.other.http.resp.FetchVertifyCodeResp;
import cn.xspace.module.other.http.resp.ForgetPasswordResp;
import cn.xspace.module.other.http.resp.ImgDownResp;
import cn.xspace.module.other.http.resp.PlaceSetResp;
import cn.xspace.module.other.http.resp.QueryAppIndexResp;
import cn.xspace.module.other.http.resp.QueryInitialDataResp;
import cn.xspace.module.other.http.resp.QueryUnionAreaResp;
import cn.xspace.module.other.http.resp.QueryUserInfoResp;
import cn.xspace.module.other.http.resp.RegisterResp;
import cn.xspace.module.other.http.resp.ResetPasswordResp;
import cn.xspace.module.other.http.resp.UpgradeResp;

public class NetControl extends ControlManage {
    public static final String MSGTYPE_SEARCH_USER = "SearchUser";
    public static final String MSGTYPE_FETCH_VERTIFYCODE = "FetchVertifyCode";
    public static final String MSGTYPE_REGISTER = "Register";
    public static final String MSGTYPE_RESETPASSWORD = "ReSetPassword";
    public static final String MSGTYPE_FORGETPASSWORD = "ForgetPassword";
    public static final String MSGTYPE_QUERY_USERINFO = "QueryUserInfo";
    public static final String MSGTYPE_PLACE_SET = "place_set";
    public static final String MSGTYPE_INFO_MODIFY_USERINFO = "ModifyUserInfo";
    public static final String MSGTYPE_INSTALL_APP = "install_app";
    public static final String MSGTYPE_UPDATE = "update";
    public static final String QUERYAPPINDEX = "queryAppIndex";
    public static final String IMGDOWN = "imgDown";
    public static final String HTML_DETAIL = "htmlDetail";

    public NetControl(SendRequestListener listener) {
        this.listener = listener;
    }

    public int sendQueryIntialData(String user_id, String app_id) {
        QueryInitialDataReq req = new QueryInitialDataReq();
        req.user_id = user_id;
        req.app_id = app_id;
        QueryInitialDataResp resp = new QueryInitialDataResp();
        super.addTask(req, resp);
        return req.serialID;
    }

    public int sendDetalHtml(String url) {
        DetalHtmlReq req = new DetalHtmlReq();
        req.url = url;
        DetailHtmlResp resp = new DetailHtmlResp();
        resp.msgType = HTML_DETAIL;
        resp.hostUrl = url;
        resp.htmlFlag = true;
        super.addTask(req, resp);
        return req.serialID;
    }

    /**
     * @param @param  mobile
     * @param @return
     * @return
     * @throws
     * @Description: 发送根据手机号码查询用户请求
     */
    public int sendQueryLoginInfoReq(String mobile, String password, String app_id) {
        QueryLoginInfoReq req = new QueryLoginInfoReq();
        req.mobile = mobile;
        req.appId = app_id;
        req.password = password;
        QueryInitialDataResp resp = new QueryInitialDataResp();
        resp.msgType = MSGTYPE_SEARCH_USER;
        super.addTask(req, resp);
        return req.serialID;
    }

//    public int sendQueryQrCodeReq(String deptid) {
//        QueryQRCodeReq req = new QueryQRCodeReq(deptid);
//        BaseGroupRelResp resp = new BaseGroupRelResp();
//        super.addTask(req, resp);
//        return req.serialID;
//    }
//
//    public int sendQueryCopyRightReq(String deptid) {
//        QueryCopyRightReq req = new QueryCopyRightReq(deptid);
//        BaseGroupRelResp resp = new BaseGroupRelResp();
//        super.addTask(req, resp);
//        return req.serialID;
//    }

    /**
     * @param @param  mobile
     * @param @return
     * @return
     * @throws
     * @Description: 查询用户权限
     */
//    public int sendQueryUserPowerReq(String uuid, String deptid) {
//        QueryUserPowerReq req = new QueryUserPowerReq(uuid, deptid);
//        QueryUserPowerResp resp = new QueryUserPowerResp();
//        resp.msgType = MSGTYPE_SEARCH_USER;
//        super.addTask(req, resp);
//        return req.serialID;
//    }

    /**
     * @param @param  mobile
     * @param @param  isRegister
     * @param @return
     * @return
     * @throws
     * @Description: 发送获取验证码请求
     */
    public int sendFetchVertifyCodeReq(String mobile, boolean isRegister) {
        FetchVertifyCodeReq req = new FetchVertifyCodeReq(isRegister);
        req.mobile = mobile;
        FetchVertifyCodeResp resp = new FetchVertifyCodeResp();
        resp.msgType = MSGTYPE_FETCH_VERTIFYCODE;
        super.addTask(req, resp);
        return req.serialID;
    }

    /**
     * @param @param  mobile
     * @param @param  pwd
     * @param @param  vertifycode
     * @param @return
     * @return
     * @throws
     * @Description: 注册
     */
    public int sendRegisterReq(String mobile, String pwd, String vertifycode) {
        RegisterReq req = new RegisterReq();
        req.mobile = mobile;
        req.pwd = pwd;
        req.vertifycode = vertifycode;
        RegisterResp resp = new RegisterResp();
        resp.msgType = MSGTYPE_REGISTER;
        super.addTask(req, resp);
        return req.serialID;
    }

    /**
     * @param @param  mobile
     * @param @param  pwd
     * @param @param  vertifycode
     * @param @return
     * @return
     * @throws
     * @Description: 重置密码
     */
    public int sendResetPasswordReq(String mobile, String pwd, String oldPwd) {
        ResetPasswordReq req = new ResetPasswordReq();
        req.mobile = mobile;
        req.pwd = pwd;
        req.old_pwd = oldPwd;
        ResetPasswordResp resp = new ResetPasswordResp();
        resp.msgType = MSGTYPE_RESETPASSWORD;
        super.addTask(req, resp);
        return req.serialID;
    }

    /**
     * @param @param  mobile
     * @param @param  pwd
     * @param @param  vertifycode
     * @param @return
     * @return
     * @throws
     * @Description: 重置密码
     */
    public int sendForgetPasswordReq(String mobile, String pwd, String vertifycode) {
        ForgetPasswordReq req = new ForgetPasswordReq();
        req.mobile = mobile;
        req.pwd = pwd;
        req.vertifycode = vertifycode;
        ForgetPasswordResp resp = new ForgetPasswordResp();
        resp.msgType = MSGTYPE_RESETPASSWORD;
        super.addTask(req, resp);
        return req.serialID;
    }

    /**
     * @param @param areacode
     * @return
     * @throws
     * @Description: 获取用户地区
     */
    public int sendPlaceRequest(String areacode) {
        PlaceSetReq req = new PlaceSetReq();
        req.areacode = areacode;
        PlaceSetResp resp = new PlaceSetResp();
        resp.msgType = MSGTYPE_PLACE_SET;
        super.addTask(req, resp);
        return req.serialID;
    }

    /**
     * @param @param accountID
     * @param @param logo
     * @param @param sex
     * @param @param address
     * @param @param desc
     * @return
     * @throws
     * @Description: 修改个人信息
     */
    public int modifyUserInfo(String accountID, String logo, String sex, String address, String desc) {
        ModifyUserInfoReq req = new ModifyUserInfoReq();
        req.accountID = accountID;
        req.logo = logo;
        req.sex = sex;
        req.address = address;
        req.desc = desc;
        BaseGroupRelResp resp = new BaseGroupRelResp();
        resp.msgType = MSGTYPE_INFO_MODIFY_USERINFO;
        super.addTask(req, resp);
        return req.serialID;

    }

    /**
     * @Description: 统计安装量
     * @param @param app_id
     * @return
     * @throws
     */
    //	public int sendInstallRequst(String app_id) {
    //		InstallReq req = new InstallReq();
    //		req.app_id = app_id;
    //		InstallResp resp = new InstallResp();
    //		resp.msgType = MSGTYPE_INSTALL_APP;
    //		super.addTask(req, resp);
    //		return req.serialID;
    //	}

    /**
     * @param @param  accountID
     * @param @return
     * @return
     * @throws
     * @Description: 发送查询用户个人信息请求
     */
    @Deprecated
    public int sendQueryUserInfoReq(String accountID, String deptid, String appid) {
        QueryUserInfoReq req = new QueryUserInfoReq();
        req.accountID = accountID;
        req.deptid = deptid;
        req.app_id = appid;
        QueryUserInfoResp resp = new QueryUserInfoResp();
        resp.msgType = MSGTYPE_QUERY_USERINFO;
        super.addTask(req, resp);
        return req.serialID;
    }

    /**
     * @param @param user_id
     * @param @param version_code
     * @return
     * @throws
     * @Description: 升级
     */
    public int sendUpdate(String user_id, String departmentID, String mobile, String app_id, String imei,
                          String baseband, String osversion, String manufacturer, String mac, String cpuinfo, String
                                  appversion,
                          String area) {
        UpgradeReq req = new UpgradeReq();
        req.user_id = user_id;
        req.imei = imei;
        req.departmentID = departmentID;
        req.mobile = mobile;
        req.app_id = app_id;
        req.baseband = baseband;
        req.osversion = osversion;
        req.manufacturer = manufacturer;
        req.mac = mac;
        req.cpuinfo = cpuinfo;
        req.appversion = appversion;
        req.area = area;
        UpgradeResp resp = new UpgradeResp();
        resp.msgType = MSGTYPE_UPDATE;
        super.addTask(req, resp);
        return req.serialID;
    }

    @Deprecated
    public int sendQueryAppIndex(String accountID, String appId, String user_type) {
        QueryAppIndexReq req = new QueryAppIndexReq();
        req.accountID = accountID;
        req.appId = appId;
        req.user_type = user_type;
        QueryAppIndexResp resp = new QueryAppIndexResp();
        resp.msgType = QUERYAPPINDEX;
        // resp.htmlFlag=true;
        super.addTask(req, resp);
        return req.serialID;
    }

    public int downImg(String url) {
        ImgDownReq req = new ImgDownReq();
        req.url = url;
        ImgDownResp resp = new ImgDownResp();
        resp.msgType = IMGDOWN;
        resp.isImg = true;
        super.addTask(req, resp);
        return req.serialID;
    }

    public int sendQueryUnionArea(String pid) {
        QueryUnionAreaReq req = new QueryUnionAreaReq(pid);
        QueryUnionAreaResp resp = new QueryUnionAreaResp();
        super.addTask(req, resp);
        return req.serialID;
    }

    @Override
    public void resultInfomation(IResponseMsg respMsg) {
        AResponseMsg resp = (AResponseMsg) respMsg;
        if (listener != null) {
            listener.requestResult(resp.result, resp);
        }
    }

}
